var config = require('../../config.js');
var common = require('../utils/common.js');
var promise = require('../utils/es6-promise.auto.min.js');
var util=require('../utils/util.js');

function transSS(lengthInSecond) {
  var minute = parseInt(lengthInSecond / 60);
  var second = parseInt(lengthInSecond - minute * 60);
  var str = "0" + minute + ":" + (second > 9 ? second : '0' + second);
  return str;
}

//加载materials记录
function load_material_page(params, tab_page, username, page) {
  wx.showLoading({
    title: '加载中···',
  });
  wx.request({
    url: config.materials + '/search/findByTagNameAndTypeAndViewerUsername',
    data: {
      tagName: config.tagName,
      username: username,
      page: params.page,
      size: params.size,
      type:'VIDEO'
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var _embedded = data._embedded;
      var materials = _embedded.materials;
      new promise(function (resolve, reject) {
        for (var i = 0; i < materials.length; i++) {
          if (materials[i].dialect == 'MANDARIN' && materials[i].type=='VIDEO') {
            materials[i].bindtap = 'inplay_material_p';
          } 
          materials[i].showTime = formateDate(materials[i].showTime);
          materials[i].day = util.getDays(materials[i].showTime);
          materials[i].isTouchMove=false;
          materials[i].checked=false;
          var lengthInSecond = materials[i].lengthInSecond;
          if (lengthInSecond) {
            materials[i].lengthInSecond = transSS(lengthInSecond);
          }
        }
        resolve(materials);
       
      }).then(function (materials) {
        var list = tab_page.material_list;
        if (list == null || undefined == list) {
          tab_page.material_list = materials;
          tab_page.page = params.page;
          tab_page.pageSize = data.page.totalPages;
        } else {
          tab_page.material_list = list.concat(materials);
          tab_page.page = params.page;
        }
        if (tab_page.material_list.length>0){
          if (!page.data.showf) {
            page.setData({ showf: true });
          }
        }
        page.setData({
          material_list: tab_page.material_list
        });
        //setTimeout(function () {
          wx.hideLoading()
        //}, 1000)
      });
    }
  });
}


//加载记录
function load_series_page(params, tab_page, username, page) {

  wx.request({
    url: config.series + '/search/findByTagNameAndViewerUsername',
    data: {
      tagName: config.tagName,
      username: username,
      page: params.page,
      size: params.size
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      var _embedded = data._embedded;
      var series = _embedded.series;

      var list = tab_page.series_list;
      if (list == null || undefined == list) {
        tab_page.series_list = series;
        tab_page.page = params.page;
      } else {
        tab_page.series_list = list.concat(series);
        tab_page.page = params.page;
        tab_page.pageSize = data.page.totalPages;
      }
      page.setData({
        series_list: tab_page.series_list
      });
    }
  });
}


//格式化时间
function formateDate(objTime) {
  var pattern = /(\d{4})(\d{2})(\d{2})/;
  var formated = objTime.replace(pattern, '$1-$2-$3');
  return formated;
}
module.exports = {
  load_series_page: load_series_page,
  load_material_page: load_material_page
}